package com.airsystem.pos.bengkel.controller;

import java.util.Vector;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.component.TableFrame;
import com.airsystem.pos.bengkel.entity.DetailKendaraan;
import com.airsystem.pos.bengkel.entity.Kendaraan;
import com.airsystem.pos.bengkel.interfaces.IMaster;
import com.airsystem.pos.bengkel.interfaces.ITable;
import com.airsystem.pos.bengkel.model.MasterVehicleModel;
import com.airsystem.pos.bengkel.service.KendaraanServices;
import com.airsystem.pos.bengkel.view.dialog.MasterVehicleDialog;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class MasterVehicleController extends MasterVehicleModel implements IMaster, ITable {
	private static final Logger LOG = Logger.getLogger(MasterVehicleController.class.getSimpleName());

	private boolean isExist;

	private String merk, subMerk;

	private KendaraanServices kendaraanServices = new KendaraanServices();

	public MasterVehicleController() {
		super();
	}

	public void setup() {
		saveButton.setVisible(true);
		updateButton.setVisible(false);
		deleteButton.setVisible(false);

		saveButton.setEnabled(false);
	}

	public void search() {
		dialogMasterVehicle = new MasterVehicleDialog();

		try {
			if (dialogMasterVehicle.getMerk() == null
			||  dialogMasterVehicle.getSubMerk().size() == 0) {
				cancel();
			} else {
				merkField.setText(dialogMasterVehicle.getMerk());

				TableFrame.deleteTable(tableVehicle, tableModel);
				for (DetailKendaraan data : dialogMasterVehicle.getSubMerk()) {
					tableModel.addRow(new String[] { data.getSub_merk() });
				}
				tableVehicle.setModel(tableModel);

				saveButton.setVisible(false);
				updateButton.setVisible(true);
				deleteButton.setVisible(true);
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	public void get() {
		merk = merkField.getText();
		subMerk = submerkField.getText();
	}

	public void reset() {
		submerkField.setText("");
	}

	public void save() {
		try {
			get();

			if (merk.isEmpty()) {
				JOptionPane.showMessageDialog(MasterVehicleController.this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
			} else {
				Kendaraan kendaraan = new Kendaraan();
				kendaraan.setMerk(merk);
				kendaraanServices.saveOrUpdate(kendaraan);

				for (int i = 0; i < tableModel.getRowCount(); i++) {
					DetailKendaraan detailKendaraan = new DetailKendaraan();
					detailKendaraan.setKendaraan(kendaraan);
					detailKendaraan.setSub_merk(tableVehicle.getValueAt(i, 0).toString());

					kendaraanServices.saveOrUpdate(detailKendaraan);
				}

				JOptionPane.showMessageDialog(MasterVehicleController.this, "Simpan data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
				cancel();
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(MasterVehicleController.this, "Simpan data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void update() {
		try {
			get();

			if (saveButton.isVisible()) {
				/* Save data state */
				edit();
			} else {
				if (merk.isEmpty()) {
					JOptionPane.showMessageDialog(MasterVehicleController.this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
				} else {
					/* Update data state */
					Kendaraan kendaraan = new Kendaraan();
					kendaraan.setMerk(merk);
					kendaraanServices.update(dialogMasterVehicle.getMerk(), merk);

					if (!subMerk.isEmpty()) {
						/* Update data per item */
						DetailKendaraan detailKendaraan = new DetailKendaraan();
						detailKendaraan.setKendaraan(kendaraan);
						detailKendaraan.setSub_merk(subMerk);

						kendaraanServices.subUpdate(tableVehicle.getValueAt(row, 0).toString(), subMerk);
						edit();
					} else {
						/* Update all new data */
						for (int i = 0; i < tableModel.getRowCount(); i++) {
							DetailKendaraan detailKendaraan = new DetailKendaraan();
							detailKendaraan.setKendaraan(kendaraan);
							detailKendaraan.setSub_merk(tableVehicle.getValueAt(i, 0).toString());

							kendaraanServices.saveOrUpdate(detailKendaraan);
						}
					}

					if (!merk.isEmpty() && subMerk.isEmpty()) {
						JOptionPane.showMessageDialog(MasterVehicleController.this, "Ubah data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
						cancel();
					}
				}
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(MasterVehicleController.this, "Ubah data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void delete() {
		try {
			get();

			if (saveButton.isVisible()) {
				/* Save data state */
				remove();
			} else {
				if (merk.isEmpty()) {
					JOptionPane.showMessageDialog(MasterVehicleController.this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
				} else {
					/* Delete data state */
					Kendaraan kendaraan = new Kendaraan();
					kendaraan.setMerk(merk);

					if (!subMerk.isEmpty()) {
						/* Delete data per item */
						DetailKendaraan detailKendaraan = new DetailKendaraan();
						detailKendaraan.setKendaraan(kendaraan);
						detailKendaraan.setSub_merk(subMerk);

						kendaraanServices.delete(detailKendaraan);
						remove();
					} else {
						/* Delete all data */
						kendaraanServices.delete(kendaraan);
					}
				}

				if (!merk.isEmpty() && subMerk.isEmpty()) {
					JOptionPane.showMessageDialog(MasterVehicleController.this, "Hapus data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
					cancel();
				}
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(MasterVehicleController.this, "Hapus data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void cancel() {
		TableFrame.deleteTable(tableVehicle, tableModel);
		merkField.setText("");
		reset();
		setup();
	}

	public void init() {
		submerkField.setText(tableVehicle.getValueAt(row, 0).toString());

		if (saveButton.isVisible()) {
			updateButton.setVisible(true);
			deleteButton.setVisible(true);
		}
	}

	public void add() {
		get();

		if (subMerk.isEmpty()) {
			JOptionPane.showMessageDialog(MasterVehicleController.this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
		} else {
			Vector<String> vector = new Vector<String>();
			vector.add(subMerk);

			check();

			if (!isExist) {
				tableModel.addRow(vector);
				tableVehicle.setModel(tableModel);

				if (saveButton.isVisible()) {
					saveButton.setEnabled(true);
				}
			} else {
				JOptionPane.showMessageDialog(MasterVehicleController.this, "Data sudah ada!", "Pesan", JOptionPane.WARNING_MESSAGE);
			}

			reset();
		}
	}

	public void edit() {
		get();

		if (subMerk.isEmpty()) {
			JOptionPane.showMessageDialog(MasterVehicleController.this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
		} else {
			Vector<String> vector = new Vector<String>();
			vector.add(subMerk);

			check();

			if (!isExist) {
				tableModel.removeRow(row);
				tableModel.addRow(vector);
				tableVehicle.setModel(tableModel);

				if (saveButton.isVisible()) {
					saveButton.setEnabled(true);
				}
			} else {
				JOptionPane.showMessageDialog(MasterVehicleController.this, "Data sudah ada!", "Pesan", JOptionPane.WARNING_MESSAGE);
			}

			reset();
		}
	}

	public void remove() {
		tableModel.removeRow(row);
		reset();

		if (tableVehicle.getRowCount() == 0) {
			cancel();
		}
	}

	private void check() {
		isExist = false;

		for (int i = 0; i < tableVehicle.getRowCount(); i++) {
			if (subMerk.equalsIgnoreCase(tableVehicle.getValueAt(i, 0).toString())) {
				isExist = true;
				break;
			}
		}
	}
}